package com.insigma.business.qrcode.soldier.util;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

    public static int toYear(Date accountDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(accountDate);
        return calendar.get(1);
    }

    public static List<Date> intersectionTime(String operator, Date startTime, Date endTime) {
        if (Strings.isNullOrEmpty(operator)) {
            return Lists.newArrayList(startTime, endTime);
        }

        if (startTime == null || endTime == null) {
            return convertOperator(operator);
        }

        List<Date> list = convertOperator(operator);
        Date stBefore = list.get(0);
        Date etBefore = list.get(1);
        return intersectionTime(stBefore, etBefore, startTime, endTime);
    }

    public static List<Date> convertOperator(String operator) {
        if (Strings.isNullOrEmpty(operator)) {
            return null;
        }

        List<Date> result = Lists.newArrayList();
        if (operator.startsWith("M")) {
            int month = Integer.valueOf(operator.replace("M", ""));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.MONTH, month - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            result.add(calendar.getTime());
            calendar.add(Calendar.MONTH, 1);
            calendar.add(Calendar.MINUTE, -1);
            result.add(calendar.getTime());
            return result;
        }
        if (operator.startsWith("Q")) {
            int quarter = Integer.valueOf(operator.replace("Q", ""));
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            switch (quarter) {
                case 1:
                    calendar.set(Calendar.MONTH, 0);
                    result.add(calendar.getTime());
                    calendar.set(Calendar.SECOND, -1);
                    calendar.set(Calendar.MONTH, 3);
                    result.add(calendar.getTime());
                    break;
                case 2:
                    calendar.set(Calendar.SECOND, 1);
                    calendar.set(Calendar.MONTH, 3);
                    result.add(calendar.getTime());
                    calendar.set(Calendar.SECOND, -1);
                    calendar.set(Calendar.MONTH, 6);
                    result.add(calendar.getTime());
                    break;
                case 3:
                    calendar.set(Calendar.SECOND, 1);
                    calendar.set(Calendar.MONTH, 6);
                    result.add(calendar.getTime());
                    calendar.set(Calendar.SECOND, -1);
                    calendar.set(Calendar.MONTH, 9);
                    result.add(calendar.getTime());
                    break;
                case 4:
                    calendar.set(Calendar.SECOND, 1);
                    calendar.set(Calendar.MONTH, 9);
                    result.add(calendar.getTime());
                    calendar.set(Calendar.SECOND, -1);
                    calendar.set(Calendar.MONTH, 12);
                    result.add(calendar.getTime());
                    break;
                default:
                    break;
            }

            return result;
        }
        if (operator.startsWith("Y")) {
            int year = Integer.valueOf(operator.replace("Y", ""));
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.YEAR, year);
            result.add(calendar.getTime());

            calendar.add(Calendar.YEAR, 1);
            calendar.set(Calendar.SECOND, -1);
            result.add(calendar.getTime());
            return result;
        }
        return null;
    }

    public static List<Date> intersectionTime(Date stBefore, Date etBefore, Date startTime, Date endTime) {
        if (stBefore.before(startTime) || stBefore.equals(startTime)) {
            if (etBefore.before(startTime) || etBefore.equals(startTime)) {
                return null;
            } else {
                if (etBefore.before(endTime) || etBefore.equals(endTime)) {
                    return Lists.newArrayList(startTime, etBefore);
                } else {
                    return Lists.newArrayList(startTime, endTime);
                }
            }
        } else {
            if (stBefore.before(endTime) || stBefore.equals(endTime)) {
                if (etBefore.before(endTime) || etBefore.equals(endTime)) {
                    return Lists.newArrayList(stBefore, etBefore);
                } else {
                    return Lists.newArrayList(stBefore, endTime);
                }
            } else {
                return null;
            }
        }
    }

    public static String idCardNoToBirthday(String idCardNo) {
        if (StringUtils.isBlank(idCardNo) || idCardNo.length() < 15) {
            return "";
        }
        String y = idCardNo.substring(6, 10);
        String m = idCardNo.substring(10, 12);
        String d = idCardNo.substring(12, 14);
        return y + "-" + m + "-" + d;
    }
}
